package io.gbznt.modules.buss.quartz;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.gbznt.common.utils.R;
import io.gbznt.modules.buss.entity.BussWeatherRecordEntity;
import io.gbznt.modules.buss.service.BussWeatherRecordService;
import net.sf.json.JSONObject;

@RestController
@RequestMapping("weather")
public class WeatherJob {

	@Autowired
    private BussWeatherRecordService bussWeatherRecordService;
    /**
     * 列表
     */
	String cityid;
	URLConnection connectionData;
	StringBuilder sb;
	BufferedReader br;
	JSONObject jsonData;
	JSONObject info;
    @RequestMapping("/getw")
    public R list(@RequestParam Map<String, Object> params) throws IOException{
      String cityid = "101011000";
       URL url = new URL("http://t.weather.sojson.com/api/weather/city/"+cityid);
       connectionData = url.openConnection();
       connectionData.setConnectTimeout(1000);
       try{
       br = new BufferedReader(new InputStreamReader(connectionData.getInputStream(), "utf-8"));
       sb = new StringBuilder();
       String line = null;
       while((line = br.readLine()) != null) {
           sb.append(line);
       }
       }catch (SocketTimeoutException e) {
       System.out.println("连接超时");
       }catch(FileNotFoundException e){
       System.out.println("加载文件出错");
       }
       String datas = sb.toString();
       jsonData = JSONObject.fromObject(datas);
      // info = jsonData.getJSONObject("weatherinfo");
       List<Map<String, Object>> list = new ArrayList<>();
       Calendar cal = Calendar.getInstance();
      // cal.add(Calendar.DAY_OF_YEAR,i-1);
       Date date = cal.getTime();
       SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
       Map map = new HashMap();
       //当前请求日期
       //更新时间
       String time = jsonData.getString("time");
       System.out.println(jsonData.getString("time"));
       JSONObject city = JSONObject.fromObject(jsonData.getString("cityInfo"));
       //城市
       String mycity = city.getString("city");
       System.out.println(city.getString("city"));
       System.out.println(jsonData.getString("data"));
       //当前温度
       JSONObject data = JSONObject.fromObject(jsonData.getString("data"));
       String wendu = data.getString("wendu");
       //空气质量
       String airQuality = data.getString("quality");
       System.out.println(data.getString("wendu"));
       //最高最低温度
       JSONObject forecast = JSONObject.fromObject(data.getString("forecast").substring(1, data.getString("forecast").length()-1));
       String high = forecast.getString("high");
       String low = forecast.getString("low");
       System.out.println(forecast.getString("high"));
       System.out.println(forecast.getString("low"));
       //风向
       String fx = forecast.getString("fx");
       String fl = forecast.getString("fl");
       System.out.println(forecast.getString("fx"));
       System.out.println(forecast.getString("fl"));
       //天气情况 晴雨
       String type = forecast.getString("type");
       System.out.println(forecast.getString("type"));
       //星期几
       String week = forecast.getString("week");
       System.out.println(forecast.getString("week"));
       
       BussWeatherRecordEntity be = new BussWeatherRecordEntity();
       be.setArea(mycity);
       be.setAirTemperature(wendu);
       be.setWeather(type);
       be.setWindSpeed(fl);
       be.setFx(fx);
       be.setWeek(week);
       be.setHigh(high);
       be.setLow(low);
       be.setAirQuality(airQuality);
       be.setCreateTime(new Date());
       
       bussWeatherRecordService.save(be);
       
        return R.ok();
    }
}